#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#define High   40              //定义游戏画面尺寸
#define Width 80

//函数外全局变量定义
int m,n;
int cells[High][Width];
int water_x ;
int water_y ;
int radius ;
int speed;         //演化的速度





void gotoxy(int x, int y) {
    COORD pos = {x,y};
    HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);// 获取标准输出设备句柄
    SetConsoleCursorPosition(hOut, pos);//两个参数分别是指定哪个窗体，具体位置
}




void startup()                                             //数据初始化
{
	int i,j;
	for(i=0;i<High;i++)
	{
		for(j=0;j<Width;j++)
		{
			cells[i][j] = rand()%2;
		}
	}
	 water_x = High/2;
     water_y = Width/2;
     radius = 10 ;
     speed = 50;

}




void show()                                                  //显示画面
{
    gotoxy(0,0);                                           //光标移动到原点位置,以重画清屏
		int i,j;
	for(i=0;i<High;i++)
	{
		for(j=0;j<Width;j++)
		{
			if(cells[i][j] == 1)
			{
				printf("*");
			}

			else
				printf(" ");

		}
		 printf("\n");
	}


}


void updateWithoutInput()                                           //与用户输入无关的更新数据
{
	int i,j;
	int tempCells[High][Width] ;              //临时存储的二维数组
	for(i=0;i<High;i++)
	{
		for(j=0;j<Width;j++)
		{
			tempCells[i][j] = 1;
		}
	int NeibourNumber = 0;
	for(i=1;i<High;i++)
	{
		for(j=1;j<Width-1;j++)
		{
			NeibourNumber = cells[i-1][j-1] + cells[i-1][j] +cells[i-1][j+1]
			                + cells[i][j-1] + cells[i][j] +cells[i][j+1]
			                + cells[i+1][j-1] + cells[i+1][j] +cells[i+1][j+1];
        if(i<water_x+radius&&i>water_x-radius&&j<water_y+10&&j>water_y-10)    //靠近水源的地方
			{
			    if (NeibourNumber == 3||NeibourNumber == 2||NeibourNumber == 4)
			 	tempCells[i][j] = 1;
			else if (NeibourNumber == 5||NeibourNumber == 6)
			 	tempCells[i][j] = cells[i][j];
			else
				tempCells[i][j] = 0;

     	}
     	else if (i<water_x+30&&i>2*water_x-30&&j<water_y+30&&j>water_y-20)
        {
            if (NeibourNumber == 3||NeibourNumber == 2)
			 	tempCells[i][j] = 1;
			else if (NeibourNumber == 4)
			 	tempCells[i][j] = cells[i][j];
			else
				tempCells[i][j] = 0;

        }
     	else
        {
            if (NeibourNumber == 3)
			 	tempCells[i][j] = 1;
			else if (NeibourNumber == 2)
			 	tempCells[i][j] = cells[i][j];
			else
				tempCells[i][j] = 0;
        }
     	}


     }

	}
	 for(i=0;i<High;i++)
	{
		for(j=0;j<Width;j++)
		{
			cells[i][j]=tempCells[i][j];
		}

	}
	Sleep( speed );

}




void updateWithInput()                                                  //与用户输入有关的更新
{
     



}

int main()
{
	//隐藏光标
    HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cci;
GetConsoleCursorInfo(hOut, &cci);
cci.bVisible = FALSE;
SetConsoleCursorInfo(hOut, &cci);



    startup();                                                               //数据初始化
    while(1)                                                            // 游戏循环执行
    {
        show();                                                    // 显示游戏画面
        updateWithoutInput();                                   // 与用户输入无关的更新数据
        updateWithInput();                                   // 与用户输入有关的更新数据

    }

return 0 ;


}







